package com.shenhua.test.rest;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

public class RestResultGenerator {
    /**
     * normal
     *
     * @param success
     * @param data
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseData<T> genResult(String success, T data,
                                                                String message) {
        ResponseData<T> result = new ResponseData<T>();
        result.setRespCode(success);
        result.setData(data);
        result.setRespDesc(message);
        return result;
    }

    /**
     * success
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseData<T> genSuccessResult(T data) {

        return genResult(ResponseEnum.SUCCESSS.getCode(), data, ResponseEnum.SUCCESSS.getDesc());
    }

    /**
     * error message
     *
     * @param errorCode error message
     * @param <T>
     * @return
     */

    public static <T> ResponseData<T> genErrorResult(BizErrorCode errorCode) {
        return genResult(errorCode.getCode(), null, errorCode.getMessage());
    }

    /**
     * error message
     *
     * @param errorCode error message
     * @param <T>
     * @return
     */

    public static <T> ResponseData<T> genErrorResult(BizErrorCode errorCode, String message) {
        if (StringUtils.isEmpty(message)) {
            return genErrorResult(errorCode);
        }

        return genResult(errorCode.getCode(), null, message);
    }

    /**
     * error
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseData<T> genErrorResult(String code, String message) {
        ResponseData<T> result = new ResponseData<T>();
        result.setRespCode(code);
        result.setRespDesc(message);
        return result;
    }

    /**
     * error
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseData<T> genErrorResult(String code, BizErrorCode bizErrorCode, String message) {
        ResponseData<T> result = new ResponseData<T>();
        result.setRespCode(code);
        if (Objects.nonNull(bizErrorCode)) {
            result.setBizCode(bizErrorCode.getCode());
            message = StringUtils.isBlank(message) ? bizErrorCode.getMessage() : message;
        }
        result.setRespDesc(message);
        return result;
    }

    /**
     * error
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseData<T> genErrorResult(String code, String message, T data) {
        ResponseData<T> result = new ResponseData<T>();
        result.setRespCode(code);
        result.setRespDesc(message);
        result.setData(data);
        return result;
    }

    /**
     * error message
     *
     * @param errorCode error message
     * @param <T>
     * @return
     */
    public static <T> ResponseData<T> genErrorResult(ResponseEnum errorCode) {

        return genResult(errorCode.getCode(), null, errorCode.getDesc());
    }


    /**
     * success no message
     *
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static ResponseData genSuccessResult() {
        return genSuccessResult(null);
    }
}
